<template>
  <view class="cart-content" v-if="cart.length">
    <!-- 使用自定义的收获地址组件 -->
    <my-address></my-address>
    <!-- 购物车标题区域 -->
    <view class="cart-title">
      <!-- 左侧图标 -->
      <view class="title-icon">
        <uni-icons type="shop" size="18"></uni-icons>
      </view>
      <!-- 右侧文本 -->
      <text class="t-text">购物车</text>
    </view>

    <!-- 商品列表区域 -->
    <uni-swipe-action>
      <block v-for="good in cart" :key="good.goods_id">
        <uni-swipe-action-item :right-options="options" @click="goodRemoveHandler(good)">
          <my-goods isShowRadio is-show-number-box :good="good" @radioClick="radioClickHandler"
            @num-box-change="changeValueHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>

<!-- 使用自定义结算组件 -->
  <my-settle></my-settle>
  </view>
  
  <!-- 购物车为空时的页面 -->
  <view class="empty-cart" v-else>
    <image class="e-img" src="../../static/cart_empty@2x.png" mode=""></image>
    <text style="font-size: 32rpx; color:gray;">空空如也</text>
  </view>
</template>

<script>
  import mixinBadge from '../../mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'

  export default {
    mixins: [mixinBadge],
    computed: {
      ...mapState('m_cart', ['cart']),
    },
    data() {
      return {
        options:[{
          text:'删除',
          style:{
             backgroundColor:'#40cc92'
          }
        }],
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodState', 'updateGoodCount','removeGood']),
      //回调函数组
      //点击了单选按钮
      radioClickHandler(good) { 
        this.updateGoodState(good);
      },
      //数字输入框的内容发生了变化
      changeValueHandler(good) {
        this.updateGoodCount(good);
      },
       //点击了滑动删除按钮
      goodRemoveHandler(good){
        this.removeGood(good);
      }
    },
  }
</script>

<style lang="scss">
  .cart-content{
    padding-bottom: 50px;
    
    .cart-title {
      display: flex;
      padding: 20rpx 10rpx;
      border-bottom: 1px solid #efefef;
    
      //左侧icon
      .title-icon {
        margin-right: 20rpx;
      }
    
      //右侧文本
      .t-text {
        font-size: 32rpx;
      }
    }
  }
  
  
  //购物车为空
  .empty-cart{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    
    .e-img{
      width: 90px;
      height: 90px;
      margin-bottom: 5px;
    }
  }
</style>